/**
 * Created with Intellij JDEA
 * Description:
 * User:
 * Date:2022-05-16
 * Time:17:07
 */
public class 后继者 {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        TreeNode cur = null;
        if(p.right != null) {
            cur = p.right;
            while(cur.left != null) {
                cur = cur.left;
            }
            return cur;
        }
        TreeNode node = root;
        while(node != null) {
            if(node.val > p.val) {
                cur = node;
                node = node.left;
            }else {
                node = node.right;
            }
        }
        return cur;
    }
}
